from django.contrib import admin
from .models import MapLocation, MapRoute, TencentMapConfig, MapSearchHistory


@admin.register(MapLocation)
class MapLocationAdmin(admin.ModelAdmin):
    """
    地图位置管理界面
    """
    list_display = ('name', 'address', 'latitude', 'longitude', 'category', 'is_active', 'created_by', 'created_at')
    list_filter = ('category', 'is_active', 'created_at')
    search_fields = ('name', 'address', 'description')
    list_editable = ('is_active',)
    ordering = ('-created_at',)
    date_hierarchy = 'created_at'
    
    fieldsets = (
        ('基本信息', {
            'fields': ('name', 'address', 'description', 'category')
        }),
        ('地理位置', {
            'fields': ('latitude', 'longitude')
        }),
        ('状态管理', {
            'fields': ('is_active', 'created_by')
        }),
    )
    
    def save_model(self, request, obj, form, change):
        if not change:  # 新建时设置创建者
            obj.created_by = request.user
        super().save_model(request, obj, form, change)


@admin.register(MapRoute)
class MapRouteAdmin(admin.ModelAdmin):
    """
    地图路线管理界面
    """
    list_display = ('name', 'start_location', 'end_location', 'travel_mode', 'distance', 'duration', 'is_favorite', 'created_by', 'created_at')
    list_filter = ('travel_mode', 'is_favorite', 'created_at')
    search_fields = ('name', 'start_location__name', 'end_location__name')
    list_editable = ('is_favorite',)
    ordering = ('-created_at',)
    date_hierarchy = 'created_at'
    
    fieldsets = (
        ('路线信息', {
            'fields': ('name', 'start_location', 'end_location', 'travel_mode')
        }),
        ('路线详情', {
            'fields': ('waypoints', 'distance', 'duration')
        }),
        ('路线数据', {
            'fields': ('route_data',),
            'classes': ('collapse',)
        }),
        ('状态管理', {
            'fields': ('is_favorite', 'created_by')
        }),
    )
    
    def save_model(self, request, obj, form, change):
        if not change:  # 新建时设置创建者
            obj.created_by = request.user
        super().save_model(request, obj, form, change)


@admin.register(TencentMapConfig)
class TencentMapConfigAdmin(admin.ModelAdmin):
    """
    腾讯地图API配置管理界面
    """
    list_display = ('service_name', 'is_active', 'daily_quota', 'used_quota', 'quota_percentage', 'last_reset_date', 'updated_at')
    list_filter = ('is_active', 'last_reset_date')
    search_fields = ('service_name', 'api_key')
    list_editable = ('is_active',)
    ordering = ('-created_at',)
    
    fieldsets = (
        ('服务配置', {
            'fields': ('service_name', 'api_key', 'secret_key')
        }),
        ('配额管理', {
            'fields': ('daily_quota', 'used_quota', 'last_reset_date')
        }),
        ('状态管理', {
            'fields': ('is_active',)
        }),
    )
    
    def quota_percentage(self, obj):
        """计算配额使用百分比"""
        if obj.daily_quota > 0:
            percentage = (obj.used_quota / obj.daily_quota) * 100
            return f"{percentage:.1f}%"
        return "0%"
    quota_percentage.short_description = '配额使用率'


@admin.register(MapSearchHistory)
class MapSearchHistoryAdmin(admin.ModelAdmin):
    """
    地图搜索历史管理界面
    """
    list_display = ('user', 'search_keyword', 'search_location', 'result_count', 'search_time')
    list_filter = ('search_time', 'user')
    search_fields = ('search_keyword', 'search_location', 'user__username')
    ordering = ('-search_time',)
    date_hierarchy = 'search_time'
    
    readonly_fields = ('search_time',)
    
    fieldsets = (
        ('搜索信息', {
            'fields': ('user', 'search_keyword', 'search_location')
        }),
        ('搜索结果', {
            'fields': ('result_count', 'search_time')
        }),
    )
